package cn.edu.ctbu.service.impl;

import cn.edu.ctbu.DTO.DishDetailDTO;
import cn.edu.ctbu.VO.DishDetailVO;
import cn.edu.ctbu.VO.PageDishVO;
import cn.edu.ctbu.context.BaseContext;
import cn.edu.ctbu.entity.Category;
import cn.edu.ctbu.entity.Dish;
import cn.edu.ctbu.entity.DishFlavor;
import cn.edu.ctbu.mapper.DishMapper;
import cn.edu.ctbu.result.PageResult;
import cn.edu.ctbu.service.ICategoryService;
import cn.edu.ctbu.service.IDishFlavorService;
import cn.edu.ctbu.service.IDishService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-12
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    ICategoryService iCategoryService;
    @Autowired
    IDishFlavorService iDishFlavorService;
    @Override
    public PageResult get(Integer page, Integer pageSize, String name, Long categoryId, Integer status) {
        Page pagewrapper=new Page<>();
        if (page!=null && pageSize!=null){
            pagewrapper.setCurrent(page).setSize(pageSize);

        }
        QueryWrapper wrapper=new QueryWrapper<>();
        if(name!=null){
            wrapper.like("name",name);
        }
        if(categoryId!=null){
            wrapper.eq("category_id",categoryId);
        }
        if(status!=null){
            wrapper.eq("status",status);
        }
        Page paged = page(pagewrapper, wrapper);
        List<Dish> dishes=paged.getRecords();
        List<PageDishVO> list=new ArrayList<>();
        for (Dish dish:dishes){
            PageDishVO vo=new PageDishVO();
            BeanUtils.copyProperties(dish,vo);
            Category byId = iCategoryService.getById(dish.getCategoryId());
            vo.setCategoryName(byId.getName());
            list.add(vo);
        }
        PageResult pageResult=PageResult.builder().total(paged.getTotal()).records(list).build();
        return pageResult;
    }

    @Override
    public void status(Long id) {
        Dish byId = getById(id);
        Integer status = byId.getStatus();
        Dish dish=new Dish();
        dish.setId(id);
        if(status==1){
            dish.setStatus(0);
        }else{
            dish.setStatus(1);
        }
        Long currentId = BaseContext.getCurrentId();
        dish.setUpdateUser(currentId);
        dish.setUpdateTime(LocalDateTime.now());
        updateById(dish);
    }

    @Override
    public DishDetailVO getDishDetail(Long id) {
        //根据ID获得菜品口味信息
        Dish dish=getById(id);
        DishDetailVO vo=new DishDetailVO();
        BeanUtils.copyProperties(dish,vo);
        QueryWrapper wrapper=new QueryWrapper<>().eq("dish_id",id);
        List<DishFlavor> flavors=iDishFlavorService.list(wrapper);


        //判断是否有口味选择
        if(flavors==null){
            return vo;
        }
        vo.setFlavors(flavors);
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(DishDetailDTO dto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
        dish.setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now())
                        .setCreateUser(BaseContext.getCurrentId()).setUpdateUser(BaseContext.getCurrentId());
        save(dish);
        List<DishFlavor> dishflavors=dto.getFlavors();
        if(dishflavors.size()>0){
        if (!Objects.equals(dishflavors.get(0).getName(), "")) {
            for (DishFlavor f:dishflavors){
                DishFlavor flavor = new DishFlavor();
                flavor.setDishId(dish.getId());
                flavor.setName(f.getName());
                flavor.setValue(f.getValue());
                iDishFlavorService.save(flavor);
            }
        }}
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void put(DishDetailDTO dto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
        dish.setUpdateUser(BaseContext.getCurrentId()).setUpdateTime(LocalDateTime.now());
        updateById(dish);

        List<DishFlavor> dishflavors=dto.getFlavors();
        QueryWrapper wrapper=new QueryWrapper<>().eq("dish_id",dish.getId());
        iDishFlavorService.remove(wrapper);
        if(dishflavors.size()>0){
        if (!Objects.equals(dishflavors.get(0).getName(), "")) {

            for (DishFlavor f:dishflavors){
                DishFlavor flavor = new DishFlavor();
                flavor.setDishId(dish.getId());
                flavor.setName(f.getName());
                flavor.setValue(f.getValue());
                iDishFlavorService.save(flavor);
            }
        }}
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void del(List<Long> ids) {
        for (Long id:ids){
            removeById(id);
            QueryWrapper wrapper=new QueryWrapper<>().eq("dish_id",id);
            iDishFlavorService.remove(wrapper);
        }
    }

    @Override
    public List<Dish> getByCategory(Long categoryId) {
        QueryWrapper wrapper=new QueryWrapper<>().eq("category_id",categoryId);
        List<Dish> list = list(wrapper);
        return list;
    }

    @Override
    public List<DishDetailVO> getDishes() {
        QueryWrapper wrapper=new QueryWrapper<>().eq("status",1);
        List<Dish> list = list(wrapper);
        List<DishDetailVO> dishes=new ArrayList<>();
        for (Dish dish:list){
            Long id=dish.getId();
            DishDetailVO dishDetail = getDishDetail(id);
            dishes.add(dishDetail);
        }
        return dishes;
    }
}
